<template>
  <div class="login">
    <!-- 左上角天气组件 -->
    <div class="weather">
      <!-- 天气图标 -->
      <el-icon class="weather-icon"><Sunny /></el-icon>
      <!-- 天气信息容器 -->
      <div class="weather-info">
        <div class="temperature">25°C</div>
        <div class="location">北京市</div>
      </div>
    </div>

    <!-- 右上角时间戳组件 -->
    <div class="timestamp">
      {{ currentTime }}
    </div>

    <!-- <el-button type="primary" size="default" @click="">点击</el-button> -->
    <el-row class="login-row">
      <el-col :span="12" class="login-left">
        <div class="logo-container">
          <svg viewBox="0 0 240 240" class="admin-logo">
            <!-- 外层圆环 -->
            <circle cx="120" cy="120" r="110" class="logo-circle" fill="none" />
            <!-- 内层圆环 -->
            <circle cx="120" cy="120" r="85" class="logo-inner-circle" />
            <!-- 动态波浪线 -->
            <path
              class="logo-wave"
              d="
                M60 120
                C80 100, 100 140, 120 120
                C140 100, 160 140, 180 120
              "
              fill="none"
            />

            <!-- 装饰点 -->
            <circle cx="60" cy="120" r="4" class="logo-dot" />
            <circle cx="180" cy="120" r="4" class="logo-dot" />

            <!-- 装饰线条 -->
            <path
              class="logo-lines"
              d="
                M90 80 L150 80
                M90 160 L150 160
              "
              stroke-linecap="round"
            />

            <!-- 文字 -->
            <text x="120" y="125" class="logo-text">Fetch</text>
            <text x="120" y="145" class="logo-subtext">LongerDay</text>
          </svg>
        </div>
      </el-col>
      <el-col :span="10" class="login-right">
        <el-form
          class="submitForm"
          label-position="right"
          label-width="80px"
          :model="ruleFormRef"
          :rules="rules"
        >
          <h2 class="title">AdminSystem</h2>
          <p class="subtitle">请登录您的账户</p>
          <div class="login-form">
            <el-form-item label="用户名" prop="username">
              <el-input
                placeholder="请输入用户名"
                prefix-icon="User"
                v-model="userName"
                @change="changeUserName"
              />
            </el-form-item>
            <el-form-item label="密码" prop="password">
              <el-input
                type="password"
                placeholder="请输入密码"
                prefix-icon="Lock"
                :show-password="true"
                :disabled="isDisabled"
                v-model="password"
                @change="changePassword"
              />
            </el-form-item>
            <div class="form-options">
              <el-checkbox>记住我</el-checkbox>
              <el-link type="primary">忘记密码？</el-link>
            </div>
            <div class="login-button">
              <el-button type="primary" class="submit-btn" @click="submitFormLogin">登录</el-button>
              <el-button class="register-btn" @click="register">注册账号</el-button>
            </div>
          </div>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted, onUnmounted, reactive } from "vue";
import { ElMessage } from "element-plus";
import "element-plus/theme-chalk/el-message.css";
import { useRouter } from "vue-router";
import https from "@/utils/https";
//获取表单数据的对象
const ruleFormRef = reactive({
  username: "",
  password: "",
});
//
const rules = reactive({
  username: [
    { required: true, min: 3, max: 10, message: "用户名长度在3-10个字符之间", trigger: "blur" },
    {
      required: true,
      pattern: /^[a-zA-Z0-9\u4e00-\u9fa5]+$/,
      message: "用户名只能包含数字、字母和文字,长度3-10个字符",
      trigger: "change",
    },
  ],
  password: [
    {
      required: true,
      pattern: /^[a-zA-Z0-9]+$/,
      min: 6,
      max: 16,
      message: "密码只能包含数字和字母,长度6-16个字符",
      trigger: "change",
    },
  ],
});

const isDisabled = ref(true);
// 用户名
const userName = ref("");
// 密码
const password = ref("");
// 路由
const router = useRouter();
// 改变用户名
const changeUserName = () => {
  // 制定校验规则
  if (userName.value !== "") {
    isDisabled.value = false;
    ruleFormRef.username = userName.value;
  } else {
    isDisabled.value = true;
  }
};
const changePassword = () => {
  ruleFormRef.password = password.value;
};
//
const submitFormLogin = async () => {
  const response = await https.post("/login", {
    username: ruleFormRef.username,
    password: ruleFormRef.password,
  });
  console.log(response);
  if (response.code === 200) {
    ElMessage({
      message: "登录成功",
      type: "success",
    });
    // 本地化存储token
    localStorage.setItem("token", response.data.token);
    // 跳转
    router.push({
      path: "/",
      query: {
        userName: ruleFormRef.username,
        password: ruleFormRef.password,
      },
    });
  } else if (response.code === 400) {
    ElMessage({
      message: "登录失败",
      type: "error",
    });
  }
};

//
const register = () => {
  router.push("/regin");
};
// 时间格式化函数：将日期对象转换为指定格式的字符串
const formatDate = (date: Date) => {
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, "0"); // 月份补零
  const day = String(date.getDate()).padStart(2, "0"); // 日期补零
  const hours = String(date.getHours()).padStart(2, "0"); // 小时补零
  const minutes = String(date.getMinutes()).padStart(2, "0"); // 分钟补零
  const seconds = String(date.getSeconds()).padStart(2, "0"); // 秒数补零

  return `${year}年${month}月${day}日 ${hours}:${minutes}:${seconds}`;
};

// 创建响应式时间变量
const currentTime = ref(formatDate(new Date()));

// 定时器引用，用于清理
let timer: number;

// 组件挂载时启动定时器，每秒更新时间
onMounted(() => {
  timer = setInterval(() => {
    currentTime.value = formatDate(new Date());
  }, 1000);
});

// 组件卸载时清除定时器，防止内存泄漏
onUnmounted(() => {
  clearInterval(timer);
});

// 天气数据（模拟数据，后续可接入实际天气API）
const weatherData = ref({
  temperature: 25,
  location: "北京市",
  weather: "晴天",
});

// 这里可以添加获取实际天气的函数
// const getWeather = async () => {
//   try {
//     const response = await fetch('weather-api-url');
//     const data = await response.json();
//     weatherData.value = data;
//   } catch (error) {
//     console.error('获取天气信息失败:', error);
//   }
// };

// onMounted(() => {
//   getWeather();
//   // ... existing mounted code ...
// });
//
//
</script>

<style scoped>
/* 全局禁止文字选中 */
.login {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
  width: 100vw;
  height: 100vh;
  background: linear-gradient(rgba(0, 0, 0, 0.5), rgba(0, 0, 0, 0.5)),
    url("@/assets/images/login-bg.jpg") no-repeat center center;
  background-size: cover;
}

/* 输入框内容可选中 */
:deep(.el-input__inner) {
  user-select: text;
  -webkit-user-select: text;
  -moz-user-select: text;
  -ms-user-select: text;
}

/* 确保天气和时间戳也不可选中 */
.weather,
.timestamp {
  user-select: none;
  -webkit-user-select: none;
  -moz-user-select: none;
  -ms-user-select: none;
}

.login-row {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 100%;
}
.login-left {
  width: 200px;
  height: 100px;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 350px; /* 与 login-right 高度一致 */
}
.login-right {
  margin-right: 100px;
  width: 400px;
  height: auto;
  min-height: 450px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(16px);
  -webkit-backdrop-filter: blur(16px);
  border: 1px solid rgba(255, 255, 255, 0.18);
  box-shadow: 0 8px 32px 0 rgba(31, 38, 135, 0.37);
  border-radius: 20px;
  padding: 20px;
}
.submitForm {
  height: 100%;
  padding: 40px;
  box-sizing: border-box;
  background: rgba(255, 255, 255, 0.05);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 16px;
}
.title {
  text-align: center;
  font-size: 28px;
  font-weight: 600;
  color: #ffffff;
  text-shadow: 0 0 10px rgba(255, 255, 255, 0.2);
  margin-bottom: 8px;
  font-family: "Arial", sans-serif;
  padding-left: 0;
}
.subtitle {
  text-align: center;
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  margin-bottom: 30px;
  padding-left: 0;
}
.el-input {
  --el-input-width: auto;
  margin-top: 0;
}
.login-form {
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: 100%;
  padding-right: 20px; /* 添加右内边距避免内容贴边 */
}
.login-button {
  margin-top: 20px;
  display: flex;
  gap: 12px;
}
.login-button {
  flex-direction: column;
}
.el-button {
  padding: 10px;
  box-sizing: border-box;
  height: 40px;
  color: #ffffff;
  font-size: 16px;
}
.el-form-item {
  margin-bottom: 20px;
  display: flex;
  align-items: center;
  width: 100%;
  gap: 10px;
}
.el-form-item__label,
:deep(.el-form-item__label) {
  color: rgba(255, 255, 255, 0.9) !important;
  font-size: 14px;
  font-weight: 500;
  text-shadow: 0 0 8px rgba(255, 255, 255, 0.1);
  line-height: 45px;
  padding: 0;
  height: 45px;
  margin-right: 0;
}
.el-form-item__content,
.form-options {
  margin-left: 90px !important;
  width: calc(100% - 90px);
}
.el-form {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.el-form-item :deep(.el-form-item__label) {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
  padding-bottom: 8px;
}
.logo-container {
  width: 300px;
  height: 300px;
  margin: 0 auto;
}
.admin-logo {
  width: 100%;
  height: 100%;
}
.logo-circle {
  stroke: rgba(255, 255, 255, 0.9);
  stroke-width: 2;
}
.logo-inner-circle {
  fill: rgba(255, 255, 255, 0.1);
  stroke: rgba(255, 255, 255, 0.6);
  stroke-width: 1;
}
.logo-wave {
  stroke: rgba(255, 255, 255, 0.9);
  stroke-width: 3;
  stroke-linecap: round;
  animation: waveFlow 3s ease-in-out infinite;
}
.logo-lines {
  stroke: rgba(255, 255, 255, 0.6);
  stroke-width: 2;
}
.logo-dot {
  fill: rgba(255, 255, 255, 0.9);
  animation: dotPulse 2s ease-in-out infinite;
}
.logo-text {
  fill: #ffffff;
  font-size: 28px;
  font-weight: bold;
  text-anchor: middle;
  font-family: "Arial", sans-serif;
}
.logo-subtext {
  fill: rgba(255, 255, 255, 0.8);
  font-size: 16px;
  font-weight: 500;
  text-anchor: middle;
  font-family: "Arial", sans-serif;
}

@keyframes waveFlow {
  0%,
  100% {
    transform: translateX(0);
  }
  50% {
    transform: translateX(10px);
  }
}

@keyframes dotPulse {
  0%,
  100% {
    transform: scale(1);
    opacity: 1;
  }
  50% {
    transform: scale(1.5);
    opacity: 0.7;
  }
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin: 10px 0;
  margin-left: 90px !important;
  width: calc(100% - 90px);
  padding: 0 10px;
}

:deep(.el-checkbox__label) {
  color: rgba(255, 255, 255, 0.8);
  font-size: 14px;
}

:deep(.el-link) {
  --el-link-font-size: 14px;
  --el-link-font-weight: 400;
  --el-link-text-color: #ffffff;
  --el-link-hover-text-color: rgba(255, 255, 255, 0.8);
}

:deep(.el-checkbox__input.is-checked + .el-checkbox__label) {
  color: #ffffff;
}

:deep(.el-checkbox__input.is-checked .el-checkbox__inner) {
  background-color: rgba(255, 255, 255, 0.9);
  border-color: rgba(255, 255, 255, 0.9);
}

:deep(.el-checkbox__inner:hover) {
  border-color: rgba(255, 255, 255, 0.8);
}

.submit-btn,
.register-btn {
  width: 100%;
  height: 45px;
  border-radius: 8px;
  font-size: 16px;
  transition: all 0.3s ease;
  margin: 0;
}

.submit-btn {
  background: linear-gradient(45deg, rgba(255, 255, 255, 0.9), rgba(255, 255, 255, 0.7));
  color: #20364d;
  border: none;
}

.submit-btn:hover {
  box-shadow: 0 5px 15px rgba(11, 164, 45, 0.2);
}

.register-btn {
  background: transparent;
  border: 1px solid rgba(255, 255, 255, 0.3);
  color: rgba(255, 255, 255, 0.9);
}

.register-btn:hover {
  background: rgba(255, 255, 255, 0.1);
}

:deep(.el-input) {
  --el-input-bg-color: rgba(255, 255, 255, 0.07);
  --el-input-border-color: rgba(255, 255, 255, 0.1);
  --el-input-hover-border-color: rgba(255, 255, 255, 0.2);
  --el-input-focus-border-color: rgba(255, 255, 255, 0.3);
  --el-input-text-color: #ffffff;
  --el-input-placeholder-color: rgba(255, 255, 255, 0.6);
  width: 100%;
}

:deep(.el-input__wrapper) {
  box-shadow: none !important;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.1);
  padding: 0 15px;
  height: 45px;
}

:deep(.el-input__wrapper:hover) {
  border-color: rgba(255, 255, 255, 0.2);
}

:deep(.el-input__wrapper.is-focus) {
  border-color: rgba(255, 255, 255, 0.3);
  box-shadow: 0 0 10px rgba(255, 255, 255, 0.1) !important;
}

.login-button {
  margin-left: 90px !important;
  width: calc(100% - 90px);
}

/* 添加时间戳样式 */
.timestamp {
  position: fixed;
  top: 20px;
  right: 30px;
  color: rgba(255, 255, 255, 0.9);
  font-size: 16px;
  font-family: "Arial", sans-serif;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

/* 天气显示样式 */
.weather {
  position: fixed;
  top: 20px;
  left: 30px;
  display: flex;
  align-items: center;
  gap: 12px;
  color: rgba(255, 255, 255, 0.9);
  font-family: "Arial", sans-serif;
  padding: 10px 20px;
  background: rgba(255, 255, 255, 0.1);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  border-radius: 8px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1);
  z-index: 1000;
}

.weather-icon {
  font-size: 24px;
  color: rgba(255, 255, 255, 0.9);
}

.weather-info {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.temperature {
  font-size: 18px;
  font-weight: 500;
}

.location {
  font-size: 14px;
  color: rgba(255, 255, 255, 0.7);
}

/* 修改输入框图标颜色 */

/* 修改输入框图标颜色 */
:deep(.el-input__prefix-icon),
:deep(.el-input__icon) {
  color: rgb(255, 255, 255) !important;
}

/* 修改密码框的小眼睛图标颜色 */
:deep(.el-input__suffix-icon) {
  color: rgb(255, 255, 255) !important;
}
</style>
